﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kosher;
using Ninject;
using Example.Base.Entities;
using Example.Base.Interfaces.Repositories;
using Example.Base.Interfaces.Validators;

namespace Example.Data.Validators
{
    public class UserValidator: Validator<User,string>, IValidator<User>
    {
        public IEnumerable<object> GetErrors()
        {
            return this.Errors.AsEnumerable();
        }

        [Inject]
        public IRepository<User> Repository { get; set; }

        #region Overrides of Validator<User,string>

        protected override void Setup()
        {
            var currentLogin = String.Empty;

            CheckThat(x => x.Login, "UserLogin")
                .Passes(x =>
                            {
                                var exist = Repository.Get(filter => filter.Login.Equals(x)).Any();
                                currentLogin = x;
                                return !exist;
                            })
                .Otherwise(String.Format(Resources.Messages.Login_0_AlreadyUse, currentLogin));
        }

        #endregion
    }
}
